//
//  SecondViewController.m
//  UI10-作业
//
//  Created by dllo on 16/6/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()<UITextFieldDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate>

@end

@implementation SecondViewController

-(void)dealloc{
    [_titleString release];
    [_number release];
    [_QQ release];
    [_picName release];
    Block_release(_Block);
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    self.title = self.titleString;
//    self.navigationController.navigationBarHidden = YES; //隐藏bar
    self.navigationItem.hidesBackButton = YES;              //隐藏按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(rightAction:)];
    [self createView];
}

- (void)createView{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 64, 207, 350)];
    imageView.backgroundColor = [UIColor yellowColor];
    imageView.tag = 1000;
    imageView.image = [UIImage imageNamed:self.picName];
    [self.view addSubview:imageView];
    [imageView release];
    
    
    UILabel *nameLable = [[UILabel alloc]initWithFrame:CGRectMake(50, 450, 50, 50)];
    nameLable.backgroundColor = [UIColor yellowColor];
    nameLable.text = @"名字:";
    [self.view addSubview:nameLable];
    [nameLable release];
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(110, 450, 250, 50)];
    textField.backgroundColor = [UIColor yellowColor];
    textField.text = self.titleString;
    textField.delegate = self;
    textField.tag = 1001;
    [self.view addSubview:textField];
    [textField release];
    
    UILabel *qqLable = [[UILabel alloc]initWithFrame:CGRectMake(50, 550, 50, 50)];
    qqLable.backgroundColor = [UIColor yellowColor];
    qqLable.text = @"QQ:";
    [self.view addSubview:qqLable];
    [qqLable release];
    UITextField *qqTextField = [[UITextField alloc]initWithFrame:CGRectMake(110, 550, 250, 50)];
    qqTextField.backgroundColor = [UIColor yellowColor];
    qqTextField.text = self.QQ;
    qqTextField.delegate = self;
    qqTextField.tag = 1002;
    [self.view addSubview:qqTextField];
    [qqTextField release];
    
    UILabel *numberLable = [[UILabel alloc]initWithFrame:CGRectMake(50, 650, 50, 50)];
    numberLable.backgroundColor = [UIColor yellowColor];
    numberLable.text = @"电话:";
    [self.view addSubview:numberLable];
    [numberLable release];
    UITextField *numberTextField = [[UITextField alloc]initWithFrame:CGRectMake(110, 650, 250, 50)];
    numberTextField.backgroundColor = [UIColor yellowColor];
    numberTextField.text = self.number;
    numberTextField.delegate = self;
    numberTextField.tag = 1003;
    [self.view addSubview:numberTextField];
    [numberTextField release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(310, 64+350-50, 70, 50);
    [button setTitle:@"换头像" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}
//键盘弹下
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
//坐标往上
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    CGFloat h = textField.center.y - self.view.frame.size.height / 2;//这个值不变化
    if ( h > 0 ) {
        self.view.center = CGPointMake(self.view.center.x, self.view.center.y - h);
    }
    return YES;
}
//坐标往下
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    CGFloat h = textField.center.y  - self.view.frame.size.height / 2;
    if (h > 0) {
        self.view.center = CGPointMake(self.view.center.x, self.view.center.y + h);
    }
    return YES;
}
//button点击事件
- (void)buttonAction:(UIButton *)button{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [actionSheet showInView:self.view];
    actionSheet.backgroundColor = [UIColor cyanColor];
    [actionSheet release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.allowsEditing = YES;
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:^{}];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImageView *imageView = [self.view viewWithTag:1000];
    [picker dismissViewControllerAnimated:YES completion:^{}];
    UIImage *image = info[UIImagePickerControllerOriginalImage];    // -->原图UIImagePickerControllerEditedImage -->编辑后的图片
    imageView.image = image;
}
//rightAction点击事件
- (void)rightAction:(UIBarButtonItem *)button{
    [self.navigationController popViewControllerAnimated:YES];
    UITextField *textField = [self.view viewWithTag:1001];
    UITextField *qqTextField = [self.view viewWithTag:1002];
    UITextField *numberTextField = [self.view viewWithTag:1003];
    NSMutableDictionary *mDic = [NSMutableDictionary dictionary];
    [mDic setValue:self.picName forKey:@"header"];
    [mDic setValue:textField.text forKey:@"name"];
    [mDic setValue:qqTextField.text forKey:@"QQ"];
    [mDic setValue:numberTextField.text forKey:@"number"];
    [self.delegate sendValue:mDic];
}




















































- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
